home *** CD-ROM | disk | FTP | other *** search
/ PC Basics 53 / PC Basics Issue 53.iso / Software / Internet / Invboard.exe / PC Basics 53 / Invboard / upload / sources / Drivers / mySQL.php
Encoding:
PHP Script  |  2002-06-12  |  8.4 KB  |  244 lines

  1. <?php
  2.  
  3. /*
  4. +--------------------------------------------------------------------------
  5. |   IBFORUMS v1
  6. |   ========================================
  7. |   by Matthew Mecham and David Baxter
  8. |   (c) 2001,2002 IBForums
  9. |   http://www.ibforums.com
  10. |   ========================================
  11. |   Web: http://www.ibforums.com
  12. |   Email: phpboards@ibforums.com
  13. |   Licence Info: phpib-licence@ibforums.com
  14. +---------------------------------------------------------------------------
  15. |
  16. |   > mySQL DB abstraction module
  17. |   > Module written by Matt Mecham
  18. |   > Date started: 14th February 2002
  19. |
  20. |    > Module Version Number: 1.0.0
  21. +--------------------------------------------------------------------------
  22. */
  23.  
  24.  
  25.  
  26. class db_driver {
  27.  
  28.     var $obj = array ( "sql_database"   => ""         ,
  29.                        "sql_user"       => "root"     ,
  30.                        "sql_pass"       => ""         ,
  31.                        "sql_host"       => "localhost",
  32.                        "sql_port"       => ""         ,
  33.                        "persistent"     => "0"         ,
  34.                        "sql_tbl_prefix"        => "ibf_"      ,
  35.                        "cached_queries" => array(),
  36.                      );
  37.                      
  38.      var $query_id      = "";
  39.      var $connection_id = "";
  40.      var $query_count   = 0;
  41.      var $record_row    = array();
  42.                   
  43.     /*========================================================================*/
  44.     // Connect to the database                 
  45.     /*========================================================================*/  
  46.                    
  47.     function connect() {
  48.     
  49.         if ($this->obj['persistent'])
  50.         {
  51.             $this->connection_id = mysql_pconnect( $this->obj['sql_host'] ,
  52.                                                    $this->obj['sql_user'] ,
  53.                                                    $this->obj['sql_pass'] 
  54.                                                 );
  55.         }
  56.         else
  57.         {
  58.             $this->connection_id = mysql_connect( $this->obj['sql_host'] ,
  59.                                                   $this->obj['sql_user'] ,
  60.                                                   $this->obj['sql_pass'] 
  61.                                                 );
  62.         }
  63.         
  64.         if ( !mysql_select_db($this->obj['sql_database'], $this->connection_id) )
  65.         {
  66.             echo ("ERROR: Cannot find database ".$this->obj['sql_database']);
  67.         }
  68.     }
  69.     
  70.     
  71.     
  72.     /*========================================================================*/
  73.     // Process a query
  74.     /*========================================================================*/
  75.     
  76.     function query($the_query) {
  77.     
  78.         //--------------------------------------
  79.         // Change the table prefix if needed
  80.         //--------------------------------------
  81.         
  82.         if ($this->obj['sql_tbl_prefix'] != "ibf_")
  83.         {
  84.            $the_query = preg_replace("/ibf_(\S+?)([\s\.,]|$)/", $this->obj['sql_tbl_prefix']."\\1\\2", $the_query);
  85.         }
  86.         
  87.         $this->query_id = mysql_query($the_query, $this->connection_id);
  88.       
  89.         if (! $this->query_id )
  90.         {
  91.             $this->fatal_error("mySQL query error: $the_query");
  92.         }
  93.         
  94.         $this->query_count++;
  95.         
  96.         $this->obj['cached_queries'][] = $the_query;
  97.         
  98.         return $this->query_id;
  99.     }
  100.     
  101.     
  102.     /*========================================================================*/
  103.     // Fetch a row based on the last query
  104.     /*========================================================================*/
  105.     
  106.     function fetch_row($query_id = "") {
  107.     
  108.         if ($query_id == "")
  109.         {
  110.             $query_id = $this->query_id;
  111.         }
  112.         
  113.         $this->record_row = mysql_fetch_array($query_id, MYSQL_ASSOC);
  114.         
  115.         return $this->record_row;
  116.         
  117.     }
  118.  
  119.     /*========================================================================*/
  120.     // Fetch the number of rows affected by the last query
  121.     /*========================================================================*/
  122.     
  123.     function get_affected_rows() {
  124.         return mysql_affected_rows($this->connection_id);
  125.     }
  126.     
  127.     /*========================================================================*/
  128.     // Fetch the number of rows in a result set
  129.     /*========================================================================*/
  130.     
  131.     function get_num_rows() {
  132.         return mysql_num_rows($this->query_id);
  133.     }
  134.     
  135.     /*========================================================================*/
  136.     // Fetch the last insert id from an sql autoincrement
  137.     /*========================================================================*/
  138.     
  139.     function get_insert_id() {
  140.         return mysql_insert_id($this->connection_id);
  141.     }  
  142.     
  143.     /*========================================================================*/
  144.     // Return the amount of queries used
  145.     /*========================================================================*/
  146.     
  147.     function get_query_cnt() {
  148.         return $this->query_count;
  149.     }
  150.     
  151.     /*========================================================================*/
  152.     // Free the result set from mySQLs memory
  153.     /*========================================================================*/
  154.     
  155.     function free_result($query_id="") {
  156.     
  157.            if ($query_id == "") {
  158.             $query_id = $this->query_id;
  159.         }
  160.         
  161.         @mysql_free_result($query_id);
  162.     }
  163.     
  164.     /*========================================================================*/
  165.     // Shut down the database
  166.     /*========================================================================*/
  167.     
  168.     function close_db() { 
  169.         return mysql_close($this->connection_id);
  170.     }  
  171.     
  172.     /*========================================================================*/
  173.     // Basic error handler
  174.     /*========================================================================*/
  175.     
  176.     function fatal_error($the_error) {
  177.         global $INFO;
  178.         
  179.         $the_error .= "\n\nmySQL error: ".mysql_error()."\n";
  180.         $the_error .= "mySQL error code: ".mysql_errno()."\n";
  181.         $the_error .= "Date: ".date("l dS of F Y h:i:s A");
  182.         
  183.         $out = "<html><head><title>Invision Board Database Error</title>
  184.                <style>P,BODY{ font-family:arial,sans-serif; font-size:11px; }</style></head><body>
  185.                 <br><br><blockquote><b>There appears to be an error with the {$INFO['board_name']} database.</b><br>
  186.                You can try to refresh the page by clicking <a href=\"javascript:window.location=window.location;\">here</a>, if this
  187.                does not fix the error, you can contact the board administrator by clicking <a href='mailto:{$INFO['email_in']}?subject=SQL+Error'>here</a>
  188.                <br><br><b>Error Returned</b><br>
  189.                <form name='mysql'><textarea rows=\"15\" cols=\"60\">".htmlspecialchars($the_error)."</textarea></form><br>We apologise for any inconvenience</blockquote></body></html>";
  190.                
  191.     
  192.         echo($out);
  193.         die("");
  194.     }
  195.     
  196.     /*========================================================================*/
  197.     // Create an array from a multidimensional array returning formatted
  198.     // strings ready to use in an INSERT query, saves having to manually format
  199.     // the (INSERT INTO table) ('field', 'field', 'field') VALUES ('val', 'val')
  200.     /*========================================================================*/
  201.     
  202.     function compile_db_insert_string($data) {
  203.     
  204.         $field_names  = "";
  205.         $field_values = "";
  206.         
  207.         foreach ($data as $k => $v) {
  208.             $v = preg_replace( "/'/", "\\'", $v );
  209.             $field_names  .= "$k,";
  210.             $field_values .= "'$v',";
  211.         }
  212.         
  213.         $field_names  = preg_replace( "/,$/" , "" , $field_names  );
  214.         $field_values = preg_replace( "/,$/" , "" , $field_values );
  215.         
  216.         return array( 'FIELD_NAMES'  => $field_names,
  217.                       'FIELD_VALUES' => $field_values,
  218.                     );
  219.     }
  220.     
  221.     /*========================================================================*/
  222.     // Create an array from a multidimensional array returning a formatted
  223.     // string ready to use in an UPDATE query, saves having to manually format
  224.     // the FIELD='val', FIELD='val', FIELD='val'
  225.     /*========================================================================*/
  226.     
  227.     function compile_db_update_string($data) {
  228.         
  229.         $return_string = "";
  230.         
  231.         foreach ($data as $k => $v) {
  232.             $v = preg_replace( "/'/", "\\'", $v );
  233.             $return_string .= $k . "='".$v."',";
  234.         }
  235.         
  236.         $return_string = preg_replace( "/,$/" , "" , $return_string );
  237.         
  238.         return $return_string;
  239.     }
  240.     
  241. } // end class
  242.  
  243.  
  244. ?>